<template>
  <el-dialog
    v-model="visible"
    v-if="load"
    :title="form.id ? '编辑' : '新增'"
    width="600"
    draggable
    @close="close"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :z-index="2000"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-align="right"
      label-width="60"
      label-position="top"
    >
      <el-row :gutter="20">
        <el-col :span="21">
          <el-form-item label="属性名" prop="name">
            <el-input v-model.trim="form.name" placeholder="请输入" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="3" class="mt-7" v-if="!form.id">
          <el-form-item label="" label-width="0" prop="status">
            <el-switch
              v-model="form.status"
              :active-value="1"
              :inactive-value="2"
              inline-prompt
              active-text="启用"
              inactive-text="禁用"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="是否危险品">
        <el-select
          v-model="form.is_dangerous"
          placeholder="是否危险品"
          class="w-full"
          clearable
        >
          <el-option
            v-for="item in whetherOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否含有电池">
        <el-select
          v-model="form.with_electricity"
          placeholder="是否含有电池"
          class="w-full"
          clearable
        >
          <el-option
            v-for="item in whetherOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注:" prop="comment">
        <el-input v-model.trim="form.comment" min-height="200" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="save(formRef)"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>
<script setup name="ComModal">
import api from "../api";
import { reactive, toRefs, getCurrentInstance } from "vue";
import { ElMessage } from "element-plus";
import { whetherOptions } from "../data.js";

const { proxy } = getCurrentInstance();
const emits = defineEmits(["query"]);
const data = reactive({
  form: {
    status: 1,
  },
  visible: false,
  loading: false,
  formRef: null,
  rules: {
    name: [{ required: true, message: "请输入属性名" }],
  },
  load: false,
});
const { rules, form, visible, loading, formRef, load } = toRefs(data);
const open = async (data) => {
  if (!load.value) {
    load.value = true;
    await 1;
  }
  if (data) form.value = data;
  else
    form.value = {
      status: 1,
      is_danger: 2,
    };
  visible.value = true;
};

const save = () => {
  if (!formRef.value) return;
  formRef.value.validate((valid) => {
    if (valid) {
      loading.value = true;
      if (form.value.id) {
        api
          .update(form.value)
          .then((res) => {
            ElMessage.success(res.message);
            emits("query");
            close();
          })
          .finally(() => {
            loading.value = false;
          });
      } else {
        api
          .add(form.value)
          .then((res) => {
            ElMessage.success(res.message);
            emits("query");
            close();
          })
          .finally(() => {
            loading.value = false;
          });
      }
    }
  });
};
const close = () => {
  proxy.resetForm("formRef");
  visible.value = false;
};
defineExpose({ open });
</script>
